import type { Slide } from '@/types/slides'
import { GetTemplateItemData } from './checkTemplate'
import { nanoid } from 'nanoid'


export async function GetGroupCourseware(tempId: string, groupName: string): Promise<Slide | undefined> {
  const tempData = await GetTemplateItemData(tempId, 'group')
  if (!tempData) return

  let groupStr = JSON.stringify(tempData.default)
  groupStr = groupStr.replace('%幻灯片章节%', groupName)

  const slideData = JSON.parse(groupStr)
  
  slideData.id = nanoid(10)
  slideData.elements.forEach((el: any) => {
    el.id = nanoid(10)
  })

  return slideData
}

export async function GetSubGroupCourseware(tempId: string, groupName: string, parentName: string): Promise<Slide | undefined> {
  const tempData = await GetTemplateItemData(tempId, 'subGroup')
  if (!tempData) return

  let groupStr = JSON.stringify(tempData.default)
  groupStr = groupStr.replace('%幻灯片章节%', parentName)
  groupStr = groupStr.replace('%幻灯片小节%', groupName)

  const slideData = JSON.parse(groupStr)

  slideData.id = nanoid(10)
  slideData.elements.forEach((el: any) => {
    el.id = nanoid(10)
  })

  return slideData
}
